package XishiBackend.controllers.android;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import XishiBackend.base.Constant;
import XishiBackend.base.Json;
import XishiBackend.base.Encrypt;
import XishiBackend.base.TimeUtil;
import XishiBackend.models.entity.Admin;
import XishiBackend.services.android.AndroidAdminService;

@RestController
@EnableAutoConfiguration
@RequestMapping("/android/admin")
public class AndroidAdminController {
    @Autowired
    private AndroidAdminService aS;

    @RequestMapping(value = "/tokenlogin", method = RequestMethod.POST)
    public String tokenlogin(@RequestParam("sig") String sig,@RequestParam("adminId") long adminId,
            @RequestParam("token") String token,
            @RequestParam("deviceToken") String deviceToken) {
        Admin a = aS.findOne(adminId);
        if(a == null) {
            return Json.format(1, "令牌登录失败，管理员不存在");
        }
        
        Date now = new Date();
        boolean tokenNotMatch = !a.getToken().equals(token);
        boolean tokenExpire = TimeUtil.string2Date(a.getLastLoginTime()).getTime() + 7 * 24 * 3600 * 1000 < now.getTime();     //7天令牌保存时间
        if(tokenNotMatch || tokenExpire) {
            return Json.format(2, "令牌登录失败，令牌无效或者已过期");
        }
        
        a.setLastLoginTime(TimeUtil.date2String(now));
        a.setDeviceToken(deviceToken);
        if(aS.save(a)) {
            Map<String, Object> r = new HashMap<String,Object>();
            r.put("personName", a.getPersonName());
            r.put("restaurantId", a.getRestaurantId());
            r.put("role", Constant.ROLE[a.getLevel()]);
            return Json.format(0, "令牌登录成功", r);
        }
        else {
            return Json.format(3, "令牌登录失败，无法保存用户信息");
        }
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(@RequestParam("sig") String sig,@RequestParam("loginName") String loginName, 
            @RequestParam("password") String password, 
            @RequestParam("deviceToken") String deviceToken) {
        Admin a = aS.findByLoginName(loginName);
        if(a == null) {
            return Json.format(1, "登录失败，管理员不存在");
        }
        
        if(!Encrypt.MD5(password).equals(a.getPassword())) {
            return Json.format(2, "登录失败，密码不正确");
        }
        
        String token = Constant.generateRandomCode(50);          //生成50位纯数字token
        a.setToken(token);
        a.setLastLoginTime(TimeUtil.now());
        a.setDeviceToken(deviceToken);
        if(aS.save(a)) {
            Map<String, Object> r = new HashMap<String,Object>();
            r.put("adminId", a.getId());
            r.put("personName", a.getPersonName());
            r.put("restaurantId", a.getRestaurantId());
            r.put("token", token);
            r.put("role", Constant.ROLE[a.getLevel()]);
            return Json.format(0, "登录成功", r);
        }
        else {
            return Json.format(3, "登录失败，保存用户令牌失败");
        }
    }
    
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public String logout(@RequestParam("sig") String sig,@RequestParam("adminId") long adminId) {
        Admin a = aS.findOne(adminId);
        if(a == null) {
            return Json.format(1, "注销失败，管理员不存在");
        }
        
        a.setToken("");
        a.setDeviceToken("");
        if(aS.save(a)) {
            return Json.format(0, "注销成功");
        }
        else {
            return Json.format(2, "注销失败，管理员保存");
        }
    }
    
    @RequestMapping(value = "/updatepassword", method = RequestMethod.POST)
    public String updatepassword(@RequestParam("sig") String sig, @RequestParam("adminId") long id, 
            @RequestParam("oldPassword") String o, @RequestParam("newPassword") String n) {
        if(o.equals(n)) {
            return Json.format(1, "修改密码失败，新密码与原密码相同");
        }
        Admin a = aS.findOne(id);
        if(a == null) {
            return Json.format(2, "修改密码失败，管理员不存在");
        }
        if(!a.getPassword().equals(Encrypt.MD5(o))) {
            return Json.format(3, "修改密码失败，原密码验证错误");
        }
        
        n = Encrypt.MD5(n);
        a.setPassword(n);
        if(aS.save(a)) {
            return Json.format(0, "修改密码成功");
        }
        else {
            return Json.format(4, "修改密码失败，新密码保存失败");
        }
    }
    
    @RequestMapping(value = "/orderstatus",method=RequestMethod.POST)
    public String orderstatus(@RequestParam("sig") String sig) {
        Map<String,String> osmap = new HashMap<String,String>();
        for(int i = 0;i < Constant.ORDER_STATUS.length;i++) {
            osmap.put("" + i, Constant.ORDER_STATUS[i]);
        }
        return Json.format(0, "获取订单状态分类成功", osmap);
    }
}
